
const app=getApp()

Page({  
  data:{  
    sid:'',  
    password:'',  
    success:'false'  
  },  
  sidInput:function(e){  
    this.setData({  
      sid:e.detail.value  
    })  
  },  
 
  passwordInput:function(e){  
    this.setData({  
      password:e.detail.value  
    })  
  },  
      
  login:function(){  
    var that =this;  
    var sid=this.data.sid;
    var password=this.data.password;
    console.log(that.data.sid.length)
    if(that.data.sid.length==0)
    {  
      wx.showToast({  
        title:'学号不能为空',  
        icon:'loading',  
        duration:500  
      })  
    }else if(that.data.password.length==0){  
      wx.showToast({  
        title: '密码不能为空',  
        icon:'loading',  
        duration:500  
      })   
  }else{  
    wx.showLoading({
      title: '登录中',
    })
    wx.cloud.callFunction({
      name:"user",
      data:{
        "type":"login",
        "username":sid,
        "password":password
      //  # "username":"用户账号" 可选参数
       // # "password":"用户密码" 可选参数
      },
      success(res){
        wx.hideLoading({
          success: (res) => {},
        })
        console.log(res)
        if (res.result.code==200){
        app.globalData.user=res.result.data.user;
        console.log(app.globalData)
        wx.switchTab({
          url: '/pages/home/home',  
        })
        
      }
        else if (res.result.code==505){
          wx.showToast({
            icon: 'none',
                title: '密码错误',
         })
        }
      },
      fail(res){
        wx.hideLoading({
          success: (res) => {},
        })
        console.log(res)
        wx.showToast({
           icon: 'none',
               title: '云函数调用失败',
        })
      }
    })
     
  }  
  },  
      
  // register:function(){  
  //   wx.navigateTo({  
  //     url: '/pages/register/register',  
  //   })  
  // },  
 
    
  forget_password:function(){  
    wx.navigateTo({  
      url: '/pages/retrievepassword/retrievepassword',  
    })  
  } 
})
